﻿using JuCheap.Core.Data;
using JuCheap.Core.Data.Entity;
using JuCheap.Core.Infrastructure;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Interfaces;
using JuCheap.Core.Models.Filters;
using JuCheap.Core.Models.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JuCheap.Core.Services.AppServices
{
    public class ReportFormService : IReportFormService
    {
        private readonly JuCheapContext _context;

        public ReportFormService(JuCheapContext context)
        {
            _context = context;
        }
        public async Task<PagedResult<ReportFormViewModel>> GetReport(OriganizationReportFilter filter)
        {
            IQueryable<Worker> workers = _context.Workers.Include(w => w.Company).Include(w => w.JobPosition);

            if (!string.IsNullOrEmpty(filter.orginationName))
            {
                workers = workers.Where(w => w.Company.Name == filter.orginationName);
            }

            if (filter.status != 0)
            {
                workers = workers.Where(w => w.Company.Status == filter.status);
            }

            var result = new List<ReportFormViewModel>();
            if (filter.reportType == 0)
            {
                // 全部人员
                var total = await workers.GroupBy(w => new { w.Company.Name, JobName = w.JobPosition.JobPositionType.Name, w.Company.Status })
                     .Select(g => new { g.Key.Name, g.Key.JobName, Count = g.Count(), g.Key.Status })
                     .Skip(filter.page * filter.rows).Take(filter.rows)
                     .ToListAsync();
                foreach (var company in total.GroupBy(a => a.Name))
                {
                    var report = new ReportFormViewModel { OrganizationName = company.Key, Employers = new Dictionary<string, int>() };
                    foreach (var item in company)
                    {
                        report.Status = item.Status;
                        report.Employers.Add(item.JobName, item.Count);
                    }
                    result.Add(report);
                }
            }
            else
            {
                var total = await workers
                    .Where(w => w.JobPosition.JobPositionType.Id == filter.reportType)
                    .GroupBy(w => new { w.Company.Name, JobName = w.JobPosition.Name, w.Company.Status })
                     .Select(g => new { g.Key.Name, g.Key.JobName, Count = g.Count(), g.Key.Status })
                     .Skip(filter.page * filter.rows).Take(filter.rows)
                     .ToListAsync();
                foreach (var company in total.GroupBy(a => a.Name))
                {
                    var report = new ReportFormViewModel { OrganizationName = company.Key, Employers = new Dictionary<string, int>() };
                    foreach (var item in company)
                    {
                        report.Status = item.Status;
                        report.Employers.Add(item.JobName, item.Count);
                    }
                    result.Add(report);
                }
            }

            return new PagedResult<ReportFormViewModel>(filter.page, filter.rows) { rows = result };
        }

        public List<CheckWorkAttendanceViewModel> GetWorkerAttend(WorkerAttendFilter filter)
        {
            IQueryable<CheckWorkAttendance> attends = _context.CheckWorkAttendances.Include(c => c.Worker);
            if (filter.WorkName.Count > 0)
            {
                //attends = attends.Where(c => c.Worker.Name == filter.WorkName);
                attends = attends.Where(c => filter.WorkName.Contains(c.Worker.Name));
            }
            if (filter.OriganizationId != 0)
            {
                attends = attends.Where(c => c.Worker.Company.Id == filter.OriganizationId);
            }
            if (!string.IsNullOrEmpty(filter.orginationName))
            {
                attends = attends.Where(c => c.Worker.Company.Name == filter.orginationName);
            }
            if (filter.status != 0)
            {
                attends = attends.Where(c => c.Worker.Status == filter.status);
            }
            if (!string.IsNullOrEmpty(filter.Date) && DateTime.TryParse(filter.Date, out var begin))
            {
                var end = begin.AddMonths(1);
                attends = attends.Where(c => c.Date >= begin && c.Date < end);
            }
            return attends.Select(a => new CheckWorkAttendanceViewModel
            {
                AddedHour = a.AddedHour,
                Date = a.Date,
                Worker = new WorkerViewModel
                {
                    Id = a.Worker.Id,
                    Name = a.Worker.Name,
                },
                Id = a.Id,
                FirstTime = a.FirstTime,
                LastTime = a.LastTime,
                TotalHour = a.TotalHour
            }).ToList();
        }
    }
}
